package com.nf.health.app.activity.myscannerdcode;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.android.volley.VolleyError;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.MyPersonalDoctorActivity;
import com.nf.health.app.adapter.MyPersonalScannerDocAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.CircleDialogFactory;
import com.nf.health.app.customview.CircleDialogFactory2;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.models.PersonalDocModel;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Jason on 2016/7/23
 * 我的私人医生
 */
public class MyScannerPersonalDoctorActivity extends BaseActivity {
    private final static String SCANNER_GETMYPERSDIONNALCOD_FLAG = "scanner.getmypersional.dco";
    public final static String SCANNER_CHANGED_DOC_FLAG = "scanner.getmypersional.changed.dco";
    public final static  String  IS_LOSE_EFFIUC =  "is_lose_effic";
    public final static  String  DEVICE_CODE =  "device_code";
    public final static  String  DOCTOR_ID =  "doctor_id";
    private CircleDialogFactory myDialog;

    @Bind(R.id.dlv_scanner_personal_doc)
    DragListView dlv_scanner_personal_doc;
    private MyPersonalScannerDocAdapter myPersonalScannerDocAdapter;
    private List<PersonalDocModel> list;
    private CircleDialogFactory2 circleDialogFactory2;
    private int isLoseEffic = 0;//  失效二维码是:0  默认私人医生(30天):1,    扫码跳转(60天) 2
    @Bind(R.id.ll_iv_miss_code)
    LinearLayout ll_iv_miss_code;//失效内容区
    String devicecode = "";//设备码
    String doctorid = "";//普通医生的id
    String privatedoctorid = "";//私人医生的id
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        isLoseEffic = getIntent().getIntExtra(IS_LOSE_EFFIUC,0);
        setContent(R.layout.activity_scanner_personal_doctor);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "私人医生");
        if(isLoseEffic == 1){//是私人医生 ,30天
            ll_iv_miss_code.setVisibility(View.GONE);
            dlv_scanner_personal_doc.setVisibility(View.VISIBLE);
            initData();
        }else if(isLoseEffic == 2){//是私人医生 ,60天
            ll_iv_miss_code.setVisibility(View.GONE);
            dlv_scanner_personal_doc.setVisibility(View.VISIBLE);
            initData();
        }else if(isLoseEffic == 0){//失效二维码
            ll_iv_miss_code.setVisibility(View.VISIBLE);
            dlv_scanner_personal_doc.setVisibility(View.GONE);
            return;
        }
        list = new ArrayList<PersonalDocModel>();
        dlv_scanner_personal_doc.setRefreshableAndLoadMoreable(false, false);
        dlv_scanner_personal_doc.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
            }
            @Override
            public void onLoadMore() {
            }
        });
    }

    /*******
     * 初始化数据
     ***/
    private void initData() {
        devicecode =  getIntent().getStringExtra(DEVICE_CODE);
        doctorid =  getIntent().getStringExtra(DOCTOR_ID);
        provider.requestGetScannerDoctor(SCANNER_GETMYPERSDIONNALCOD_FLAG);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (SCANNER_GETMYPERSDIONNALCOD_FLAG.equals(actionName)) {
            list = (List<PersonalDocModel>) object;
            if (myPersonalScannerDocAdapter == null) {
                myPersonalScannerDocAdapter = new MyPersonalScannerDocAdapter(MyScannerPersonalDoctorActivity.this, list,isLoseEffic);
                dlv_scanner_personal_doc.setAdapter(myPersonalScannerDocAdapter);
            } else {
                myPersonalScannerDocAdapter.setMynotifyDataChanged(list);
            }
            myPersonalScannerDocAdapter.setOnMyClickerListener(new OnMyClickListener() {
                @Override
                public void onClick(int position, String flag) {
                    privatedoctorid = list.get(position).getDoctorid();
                    if(isLoseEffic == 2){
                        provider.requestChangedDoctor(devicecode, doctorid, privatedoctorid, SCANNER_CHANGED_DOC_FLAG);
                    }else if(isLoseEffic == 1){
                        doctorid = "";
                        provider.requestChangedDoctor(devicecode, doctorid, privatedoctorid, SCANNER_CHANGED_DOC_FLAG);
                    }
                    /*if(myDialog == null){
                        myDialog = new CircleDialogFactory(MyScannerPersonalDoctorActivity.this);
                        myDialog.setCircleDialogFactoryMsg("何攀已经成为您的私人医生");
                        myDialog.setCirclrDialogButtonText("确定");
                        myDialog.setCircleDialogFactoryOnClicklistener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (myDialog.isShowing()) {
                                    myDialog.dismiss();
                                }
                            }
                        });
                        myDialog.setCircleDialogAtter(0.75, 0.35);
                        myDialog.getContentMsgCenter();
                        myDialog.setCancelable(true);
                        //myDialog.setOnKeyListener(onKeyListener);//监听返回键
                    }
                    if(!myDialog.isShowing()){
                        myDialog.show();
                    }*/
                }
            });
        }else if(actionName.equals(SCANNER_CHANGED_DOC_FLAG)){
            ActivityUtils.showActivity(MyScannerPersonalDoctorActivity.this, MyPersonalDoctorActivity.class);
            finish();
            /*if(myDialog == null){
                myDialog = new CircleDialogFactory(MyScannerPersonalDoctorActivity.this);
                myDialog.setCircleDialogFactoryMsg("请对医生进行评价");
                myDialog.setCircleDialogFactoryOnClicklistener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityUtils.showActivity(MyScannerPersonalDoctorActivity.this, MyPersonalDoctorActivity.class);
                        if (myDialog.isShowing()) {
                            myDialog.dismiss();
                        }
                    }
                });
                myDialog.setCircleDialogAtter(0.75, 0.35);
                myDialog.getContentMsgCenter();
                myDialog.setCancelable(false);
                //myDialog.setOnKeyListener(onKeyListener);//监听返回键
            }
            if(!myDialog.isShowing()){
                myDialog.show();
            }*/
        }
    }
    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
        if(actionName.equals(SCANNER_CHANGED_DOC_FLAG)){

        }
    }
    protected void onResume() {
        super.onResume();
    }
}